package com.aicheyoudao;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.aicheyoudao.utils.SearchUtils;
import com.aicheyoudao.utils.SearchUtils.searchResListener;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.PoiResult;

public class SearchMainActivity extends Activity implements TextWatcher, searchResListener {

	private Button search;
	private ListView hintList;
	private SearchUtils searchUtil;
	private BDLocation mLocation;
	private ArrayList<PoiInfo> listData;
	private SearchHintAdapter adapter;
	private EditText inputSearch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_main);
		searchUtil = SearchUtils.getSearchUtils();
		searchUtil.setsearchResListener(this);
		mLocation = MyApplication.getInstance().getMlocation();
		initView();
	}

	private void initView() {
		inputSearch = (EditText) findViewById(R.id.map_search_key);
		inputSearch.addTextChangedListener(this);
		search = (Button) findViewById(R.id.search_button);
		hintList = (ListView) findViewById(R.id.hint_history_list);
		listData = new ArrayList<PoiInfo>();
		adapter = new SearchHintAdapter(this, listData);
		hintList.setAdapter(adapter);
		hintList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String name = listData.get(position).name;
				Intent intent = new Intent(SearchMainActivity.this, MainMap.class);
				// double[] local = new double[2];
				// local[0] = mLatLng.latitude;
				// local[1] = mLatLng.longitude;
				intent.putExtra("searchKey", name);
				startActivity(intent);

				MyApplication.getInstance().getDb().insert(name);
			}
		});
		showHistory();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (inputSearch.getText().length() > 0) {
			search.setVisibility(View.VISIBLE);
			searchUtil.PoiNearSearch(new LatLng(mLocation.getLatitude(), mLocation.getLongitude()), inputSearch.getText().toString());
		} else {
			search.setVisibility(View.GONE);
			showHistory();
		}
	}

	@Override
	public void afterTextChanged(Editable s) {

	}

	@Override
	public void searchSucess(PoiResult res) {
		List<PoiInfo> data = res.getAllPoi();
		listData.clear();
		listData.addAll(data);
		adapter.notifyDataSetChanged();
	}

	private void showHistory() {
		List<PoiInfo> data = MyApplication.getInstance().getDb().queryAll();
		listData.clear();
		listData.addAll(data);
		adapter.notifyDataSetChanged();
	}
}
